package zhang.automobileservice;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.nineoldandroids.animation.ObjectAnimator;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import adapter.SecondCarAdapter;
import bean.Second_Car;
import utils.MyStoreDao;
import utils.MyUrl;
import utils.RequestQueueUtils;

public class MystoreActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private ImageView iv_select;
    private ImageView iv_delect;
    private ListView lv_friend;
    private TextView tv_storeSecond;
    private TextView tv_storeMaintain;
    private ArrayList<String> arrayList;
    private Second_Car secondCar;
    private ArrayList<Second_Car> beanList;
    private SecondCarAdapter adapter;
    private int listSize;
    private int i;
    Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x123) {
                secondCar = (Second_Car) msg.obj;
                beanList.add(secondCar);
            }
            if (beanList.size() == arrayList.size()) {
                adapter = new SecondCarAdapter(getApplicationContext(), beanList, arrayList);
                lv_friend.setAdapter(adapter);
            }
        }
    };
    private ListView lv_collectMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mystore);
        initView();
        MyStoreDao dao = new MyStoreDao(this);
        arrayList = dao.queryAll();
        initData();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.tv_storeSecond:
                tv_storeSecond.setVisibility(View.VISIBLE);
                tv_storeMaintain.setVisibility(View.GONE);
                listSize = lv_friend.getChildCount();
                break;
            case R.id.tv_storeMaintain:
                tv_storeSecond.setVisibility(View.GONE);
                tv_storeMaintain.setVisibility(View.VISIBLE);
                listSize = lv_collectMain.getChildCount();
                break;
            case R.id.iv_select:

                for (i = 0; i < listSize; i++) {
                    View view = lv_friend.getChildAt(i);
                    RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.rl_move);
                    ObjectAnimator anim = ObjectAnimator.ofFloat(relativeLayout, "translationX", 0, -300);
                    anim.setDuration(500);
                    anim.start();
                    iv_delect.setVisibility(View.VISIBLE);
                    iv_select.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.iv_delect:
                for (i = 0; i < listSize; i++) {
                    View view = lv_friend.getChildAt(i);
                    RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.rl_move);

                    iv_delect.setVisibility(View.INVISIBLE);
                    iv_select.setVisibility(View.VISIBLE);
                    ObjectAnimator anim = ObjectAnimator.ofFloat(relativeLayout, "translationX", -300, 0);
                    anim.setDuration(500);
                    anim.start();
                }
                break;
        }
    }

    private void initData() {
        beanList = new ArrayList<>();
        String url = MyUrl.Font_Url + MyUrl.queryById;
        for (int i = 0; i < arrayList.size(); i++) {
            RequestQueueUtils.exectueJsonRequest(this, url + arrayList.get(i), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject o) {
                    secondCar = new Second_Car();
                    try {
                        if ("0".equals(o.getString("code"))) {
                            JSONObject obj = o.getJSONObject("data");
                            secondCar.setId(obj.getInt("id") + "");
                            secondCar.setTitle(obj.getString("title"));
                            secondCar.setPrice(obj.getDouble("price") + "");
                            secondCar.setKm(obj.getString("km"));
                            secondCar.setBuyDate(obj.getString("buyDate"));
                            secondCar.setImage(obj.getString("image"));
                            secondCar.setTab(obj.getString("tab"));
                        }
                        Message msg = new Message();
                        msg.what = 0x123;
                        msg.obj = secondCar;
                        hander.sendMessage(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
        }
    }

    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        lv_friend = (ListView) findViewById(R.id.lv_friend);
        finish.setOnClickListener(this);
        tv_storeSecond = (TextView) findViewById(R.id.tv_storeSecond);
        tv_storeSecond.setOnClickListener(this);
        tv_storeMaintain = (TextView) findViewById(R.id.tv_storeMaintain);
        tv_storeMaintain.setOnClickListener(this);
        iv_select = (ImageView) findViewById(R.id.iv_select);
        iv_select.setOnClickListener(this);
        iv_delect = (ImageView) findViewById(R.id.iv_delect);
        iv_delect.setOnClickListener(this);
        lv_collectMain = (ListView) findViewById(R.id.lv_collectMain);
    }
}
